#ifndef GLTEXTURE_H
#define GLTEXTURE_H

#include <qgl.h>
#include <QVector>

class GLTexture
{
public:
    typedef enum _MinificationFilterValue
    {
        MinificationNearest = GL_NEAREST,
        MinificationLinear = GL_LINEAR,
        MinificationNearestMipMapNearest = GL_NEAREST_MIPMAP_NEAREST,
        MinficationLinearMipMapNearest = GL_LINEAR_MIPMAP_NEAREST,
        MinificationNearestMipMapLinear = GL_NEAREST_MIPMAP_LINEAR,
        MinificationLinearMipMapLinear = GL_LINEAR_MIPMAP_LINEAR
    } MinificationFilterValue;

    typedef enum _MagnificationFilterValue
    {
        MagnificationNearest = GL_NEAREST,
        MagnificationLinear = GL_LINEAR
    } MagnificationFilterValue;

    typedef enum _WrapValue
    {
        WrapRepeat = GL_REPEAT,
        WrapClampToEdge = GL_CLAMP_TO_EDGE,
        WrapMirroredRepeat = GL_MIRRORED_REPEAT
    } WrapValue;

    typedef enum _TargetValue
    {
        Target2D = GL_TEXTURE_2D,
        TargetCubeMap = GL_TEXTURE_CUBE_MAP
    } TargetValue;

    typedef enum _FormatValue
    {
        FormatAlpha = GL_ALPHA,
        FormatLuminence = GL_LUMINANCE,
        FormatLuminenceAlpha = GL_LUMINANCE_ALPHA,
        FormatRGB = GL_RGB,
        FormatRGBA = GL_RGBA
    } FormatValue;

    GLTexture(const unsigned char *data, unsigned texWidth, unsigned texHeight, FormatValue format, TargetValue target);
    virtual ~GLTexture();

    void addMipMapLevel(GLint level, GLsizei width, GLsizei height, GLubyte *pixels);

    void setTexWidth(GLsizei width) { _texWidth = width; }
    GLsizei texWidth() { return _texWidth; }
    void setTexHeight(GLsizei height) { _texHeight = height; }
    GLsizei texHeight() { return _texHeight; }
    GLuint textureId() { return _textureId; }
    void bind();
    void activate(int asTextureUnit);
    void setMinificationFilter(MinificationFilterValue value = MinificationNearestMipMapLinear);
    void setMagnificationFilter(MagnificationFilterValue value = MagnificationLinear);
    void setSCoordinateWrap(WrapValue value = WrapRepeat);
    void setTCoordinateWrap(WrapValue value = WrapRepeat);
    static void setMaxTextureUnits(int maxTextureUnits);

private:
    GLuint _textureId;
    GLsizei _texWidth;
    GLsizei _texHeight;
    GLenum _format;
    TargetValue _target;
    static QVector<GLuint> activeTextureIds;
};

#endif // GLTEXTURE_H
